library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity xmt_port_monitor is
	port(
		reset, clk: in std_logic;
		
		--test signals
--		hs: out std_logic;
		
		
		--control path inputs from xmt port
		xmt_wordsused: in std_logic_vector(12 downto 0);
		
		--control path inputs from SF_xmt_interface
		SF_PacketComing, SF_PacketFinished: in std_logic;
		SF_PacketLength: in std_logic_vector(11 downto 0);
		
		--data path inputs from SF_xmt_interface
		SF_dwtreq, SF_lwtreq: in std_logic;
		
		--data path outputs to xmt port
		xmt_dwtreq, xmt_lwtreq: out std_logic

	);
end xmt_port_monitor;

architecture arch of xmt_port_monitor is
	type STATE is (RST, IDLE, DROP, PT); --BLK excluded
	--Reset
	--Idle
	--Drop (no space for packet in output queue)
	--Block STATE NOT USED (this packet is not destined for this output port)
	--Pass Through (allow this packet to be written to this output port)
	
	signal state_cur : STATE;
	signal state_next : STATE;
	
	signal has_space : std_logic;
	
--	signal rcv0_pktrdy, rcv1_pktrdy, rcv2_pktrdy, rcv3_pktrdy: std_logic;
--	signal data_rdreq, length_rdreq: std_logic_vector(3 downto 0);
	
	begin
	--register
	process(clk, reset)
	begin
		if reset='1' then
			state_cur <= RST;
		elsif (clk'event and clk='1') then
			state_cur <= state_next;
		end if;
	end process;
	--next state logic
	state_next <=
		IDLE	when	(state_cur=RST) else
		IDLE	when 	(state_cur=DROP and SF_PacketFinished='1') else
		IDLE	when	(state_cur=PT and SF_PacketFinished='1') else
		IDLE	when	(state_cur=IDLE and SF_PacketComing='0') else
		
		DROP	when	(state_cur=IDLE and SF_PacketComing='1' and has_space='0') else
		DROP	when	(state_cur=DROP and SF_PacketComing='0') else
		
		PT		when	(state_cur=IDLE and SF_PacketComing='1' and has_space='1') else
		PT		when	(state_cur=PT and reset='0') else

		RST; --handle exceptions


	--input logic
	has_space <=
		'1' when ((8191 - unsigned(xmt_wordsused)) > (unsigned(SF_PacketLength))) else
		'0';


	--output logic

	xmt_dwtreq <=
		SF_dwtreq when (state_cur=PT) else
		'0';
		
	xmt_lwtreq <=
		SF_lwtreq when (state_cur=PT) else
		'0';
	
--	hs <= has_space;
	
end arch;